using UnityEditor;
using UnityEngine;

namespace FShaderInspector
{
    public partial class FShaderGUI
    {
        /// <summary> 高级选项绘制 </summary>
        void AdvancedOptions()
        {
            var btnRect = GetTempRect();

            var key = EditorCache.instance.GetPropertyKey(_material, "Advanced Options");
            var open = EditorCache.instance.GetKey(key);

            var cacheIcon = EditorGUIUtility.IconContent("d_CustomTool");
            cacheIcon.tooltip = "清理材质球冗余缓存记录。";
            if (GUI.Button(new Rect(btnRect.width - 30, btnRect.y + 1, 29, btnRect.height), cacheIcon))
                MaterialCleanupMenu.ClearMaterial(_materials);

            // 折页按钮
            if (GUI.Button(btnRect, "", "BoldLabel"))
                EditorCache.instance.SetKey(key, !open);

            var icon = EditorGUIUtility.IconContent(open ? "IN foldout on" : "d_IN_foldout");
            icon.text = Localization.FindName("Advanced Options");
            DrawFoldoutGUI(btnRect, icon, new Color(0.75f, 0.75f, 0.75f, 1), true);

            if (!open)
                return;

            // 兼容 Unity Lit 可选项
            if (_material.HasProperty(CommonProperty.SpecularHighlights) && _material.HasProperty(CommonProperty.EnvironmentReflections))
            {
                _materialEditor.ToggleShaderProperty(_specularHighlightsProp, Localization.FindName(_specularHighlightsProp.displayName));
                _materialEditor.ToggleShaderProperty(_environmentReflectionsProp, Localization.FindName(_environmentReflectionsProp.displayName));
            }

            // 绘制调节队列的控件 可选项
            if (_material.HasProperty(CommonProperty.QueueOffset))
            {
                EditorGUI.BeginChangeCheck();
                _materialEditor.IntSliderShaderProperty(_queueOffsetProp, -50, 50, Localization.FindName("Sorting Priority"));
                if (EditorGUI.EndChangeCheck() || _firstEnable)
                {
                    foreach (var mat in _materials)
                    {
                        var queueTag = mat.GetTag(CommonTags.Queue, true);
                        var queueOffset = (int)TryGetMatFloat(mat, CommonProperty.QueueOffset);

                        if (TryGetMatFloatAsBool(mat, CommonProperty.SurfaceType))
                            SetTransparentQueueTags(mat, queueTag, queueOffset);
                        else
                        {
                            if (TryGetMatFloatAsBool(mat, CommonProperty.AlphaClip))
                                SetOpaqueQueueTags(mat, true, queueTag, queueOffset);
                            else
                                SetOpaqueQueueTags(mat, false, queueTag, queueOffset);
                        }
                    }
                }
            }
            else
            {
                _materialEditor.RenderQueueField();
            }

            _materialEditor.EnableInstancingField();
        }
    }
}